package com.zzj.springbootlearn.autowirecapablebeanfactory;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author AJ
 * @date 2020 2020-11-19 16:29
 */
@Component
public class AutoCreateBean implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void docreate() {
        AutowireCapableBeanFactory autowireCapableBeanFactory = applicationContext.getAutowireCapableBeanFactory();
        TestBean testBean = (TestBean) autowireCapableBeanFactory.createBean(TestBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
        testBean.setName("aa");
        System.out.println(testBean);
    }

}
